<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="货位序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="postionId">
              <a-input v-model="model.postionId" placeholder="请输入货位序号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="货柜名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerName">
              <a-input v-model="model.containerName" placeholder="请输入货柜名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="货柜子ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerId">
              <a-input v-model="model.containerId" placeholder="请输入货柜子ID"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              v-if="model.id"
              label="货柜编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="containerNo"
            >
              <a-input v-model="model.containerNo" placeholder="请输入货柜编号"></a-input>
            </a-form-model-item>
            
            <a-form-model-item v-else label="货柜编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerNo">
              <a-select v-model:value="model.containerNo" placeholder="请选择货柜编号">
                <a-select-option v-for="item in containerList" :key="item.id" :value="item.containerNo"
                  >{{ item.containerNo }}-{{ item.containerName }}- 货位数量{{ item.postionSums }}</a-select-option
                >
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="货柜类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerType">
              <a-input v-model="model.containerType" placeholder="请输入货柜类型"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="条码标识" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="containerBarCode">
              <a-input v-model="model.containerBarCode" placeholder="请输入条码标识"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="udi" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="udi">
              <a-input v-model="model.udi" placeholder="请输入udi"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="品码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zxxsdycpbs">
              <a-input v-model="model.zxxsdycpbs" placeholder="请输入品码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cpmctymc">
              <a-input v-model="model.cpmctymc" placeholder="请输入产品名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ggxh">
              <a-input v-model="model.ggxh" placeholder="请输入规格"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="重量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weight">
              <a-input-number v-model="model.weight" placeholder="请输入重量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <!-- <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <a-input v-model="model.state" placeholder="请输入状态"></a-input>
            </a-form-model-item> -->
            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <a-radio-group v-model:value="model.state">
                <a-radio value="1">已绑定</a-radio>
                <a-radio value="2">未绑定</a-radio>
              </a-radio-group>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="订单号no" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderNo">
              <a-input v-model="model.orderNo" placeholder="请输入订单号no"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="订单号id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderId">
              <a-input v-model="model.orderId" placeholder="请输入订单号id"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="生产厂家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manufacture">
              <a-input v-model="model.manufacture" placeholder="请输入生产厂家"></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="注册证" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zczbhhzbapzbh">
              <a-input v-model="model.zczbhhzbapzbh" placeholder="请输入注册证"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'BmsContainerDetailForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        postionId: [{ required: true, message: '请输入货位序号!' }],
        containerName: [{ required: true, message: '请填写货柜名称!' }],
        containerCode: [{ required: true, message: '请填写货位序号!' }],
        containerNo: [{ required: true, message: '请选择货位序号!' }],
      },
      url: {
        add: '/bms/bmsContainerDetail/add',
        edit: '/bms/bmsContainerDetail/edit',
        queryById: '/bms/bmsContainerDetail/queryById',
      },
      containerList: [],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add(list) {
      this.containerList = list
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
